/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.hid.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.hid.dto.RiskHidCheckPlanDTO;
import com.hopes.bizz.risk.hid.entity.RiskHidCheckPlanEntity;
import com.hopes.bizz.risk.hid.service.RiskHidCheckPlanService;
import com.hopes.bizz.risk.hid.vo.RiskHidCheckPlanVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 隐患检查计划
 *
 * @author daijie
 * @date 2023-07-27 09:57:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskHidCheckPlan")
@Tag(description = "riskHidCheckPlan", name = "隐患检查计划管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskHidCheckPlanController {

    private final RiskHidCheckPlanService riskHidCheckPlanService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param riskHidCheckPlan 隐患检查计划
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_plan_view')")
    public R getRiskHidCheckPlanPage(Page page, RiskHidCheckPlanDTO riskHidCheckPlan) {
        LambdaQueryWrapper<RiskHidCheckPlanEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(riskHidCheckPlan.getPlanName()), RiskHidCheckPlanEntity::getPlanName, riskHidCheckPlan.getPlanName());
        wrapper.eq(ObjectUtil.isNotNull(riskHidCheckPlan.getRunFlag()), RiskHidCheckPlanEntity::getRunFlag, riskHidCheckPlan.getRunFlag());

        Page pageInfo = riskHidCheckPlanService.page(page, wrapper);
        List<RiskHidCheckPlanVO> voList = new ArrayList<>();
        for (Object record : pageInfo.getRecords()) {
            RiskHidCheckPlanVO vo = new RiskHidCheckPlanVO();
            BeanUtils.copyProperties(record, vo);
            voList.add(vo);
        }
        pageInfo.setRecords(voList);
        return R.ok(pageInfo);
    }

    /**
     * 通过id查询隐患检查计划
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_plan_view')")
    public R getById(@PathVariable("id") Long id) {
        RiskHidCheckPlanEntity entity = riskHidCheckPlanService.getById(id);
        RiskHidCheckPlanVO vo = new RiskHidCheckPlanVO();
        BeanUtils.copyProperties(entity, vo);
        return R.ok(vo);
    }

    /**
     * 新增隐患检查计划
     *
     * @param riskHidCheckPlan 隐患检查计划
     * @return R
     */
    @Operation(summary = "新增隐患检查计划", description = "新增隐患检查计划")
    @SysLog("新增隐患检查计划")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_plan_add')")
    public R save(@RequestBody RiskHidCheckPlanDTO riskHidCheckPlan) {
        return R.ok(riskHidCheckPlanService.save(riskHidCheckPlan.toPO()));
    }

    /**
     * 修改隐患检查计划
     *
     * @param riskHidCheckPlan 隐患检查计划
     * @return R
     */
    @Operation(summary = "修改隐患检查计划", description = "修改隐患检查计划")
    @SysLog("修改隐患检查计划")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_plan_edit')")
    public R updateById(@RequestBody RiskHidCheckPlanDTO riskHidCheckPlan) {
        return R.ok(riskHidCheckPlanService.updateById(riskHidCheckPlan.toPO()));
    }

    /**
     * 通过id删除隐患检查计划
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除隐患检查计划", description = "通过id删除隐患检查计划")
    @SysLog("通过id删除隐患检查计划")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_plan_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskHidCheckPlanService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     *
     * @param riskHidCheckPlan 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hid_plan_export')")
    public List<RiskHidCheckPlanEntity> export(RiskHidCheckPlanEntity riskHidCheckPlan) {
        return riskHidCheckPlanService.list(Wrappers.query(riskHidCheckPlan));
    }

    @Operation(summary = "停用任务计划设置", description = "停用任务计划设置")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_plan_edit')")
    @PostMapping(value = "startPlan/{id}")
    public R startPlan(@PathVariable Long id) {
        riskHidCheckPlanService.startPlan(id);
        return R.ok();
    }

    @Operation(summary = "停用任务计划设置", description = "停用任务计划设置")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_plan_edit')")
    @PostMapping(value = "stopPlan/{id}")
    public R stopPlan(@PathVariable Long id) {
        riskHidCheckPlanService.stopPlan(id);
        return R.ok();
    }
}